package com.ry.est.service.impl;

import com.ry.est.constants.Constants;
import com.ry.est.dao.PayOrderDao;
import com.ry.est.dto.PayOrderDTO;
import com.ry.est.entity.PayOrder;
import com.ry.est.service.PayOrderService;
import com.ry.est.util.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @author ry
 * @since 2021-11-16 10:55
 **/
@Service
public class PayOrderServiceImpl implements PayOrderService {

    @Autowired
    private PayOrderDao payOrderDao;

    @Override
    public int savePayOrder(PayOrderDTO payOrderDTO) {
        PayOrder payOrder = BeanUtils.convertToDTO(payOrderDTO, PayOrder.class);
        payOrder.setStatus(Constants.Order.PayStatus.UN_PAY);
        payOrder.setIsDelete(Constants.EST_NOT);
        payOrder.setCreatedTime(new Date());
        payOrder.setCreatedUserId(payOrderDTO.getCreatedUserId());

        int id = payOrderDao.insertSelective(payOrder);
        payOrderDTO.setId((long) id);
        return id;
    }

    @Override
    public PayOrderDTO getByPayNo(String payNo) {
        PayOrderDTO payOrderDTO = payOrderDao.selectByPayNo(payNo);

        return payOrderDTO;
    }

    @Override
    public int updateOrderStatusToSuccess(Long id, String tradeNo) {
        PayOrder payOrder = new PayOrder();
        payOrder.setId(id);
        payOrder.setAliTradeNo(tradeNo);
        payOrder.setUpdatedTime(new Date());
        payOrder.setStatus(Constants.Order.PayStatus.SUCCESS);

        payOrderDao.updateByPrimaryKeySelective(payOrder);

        return 0;
    }
}
